package com.lj.frame.menu.repo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.frame.menu.entity.Menu;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service
public class MenuRepository extends ServiceImpl<MenuMapper, Menu> {

    /**
     * 查询子一级ID
     * */
    public List<Integer> queryChildIdListByPid(List<Integer> idList) {
        LambdaQueryWrapper<Menu> query = new LambdaQueryWrapper<>();
        query.in(Menu::getPid, idList);
        return getAndConvertInteger(query);
    }

    public List<Integer> queryChildIdListByPid(Integer id) {
        LambdaQueryWrapper<Menu> query = new LambdaQueryWrapper<>();
        query.eq(Menu::getPid, id);
        return getAndConvertInteger(query);
    }

    @NotNull
    private List<Integer> getAndConvertInteger(LambdaQueryWrapper<Menu> query) {
        query.select(Menu::getId);
        List<Integer> childIdList = new ArrayList<>();
        List lists = getBaseMapper().selectObjs(query);
        if (Objects.nonNull(lists)){
            lists.forEach( obj ->{
                childIdList.add((Integer) obj);
            });
        }
        return childIdList;
    }
}
